package com.one.react.util.javac;

import com.one.react.util.reflect.Reflect;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class TypeEnvUtils {

    @SuppressWarnings("unchecked")
    public static Collection<JCTree.JCCompilationUnit> getTrees(Context context) {
        List<JCTree.JCCompilationUnit> result = new ArrayList<>();
        Collection<Env<AttrContext>> envs = (Collection<Env<AttrContext>>) Reflect.on("com.sun.tools.javac.comp.TypeEnvs")
                .call("instance", context)
                .call("values")
                .get();
        for (Env<AttrContext> env : envs) {
            result.add(env.toplevel);
        }
        return result;
    }

}
